package com.example.fw;

import java.util.List;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import com.example.tests.ContactData;
import com.example.utils.SortedListOf;

public class ContactHelper extends HelperBase {

	public static boolean CREATION = true;
	public static boolean MODIFICATION = false;
	
	public ContactHelper(ApplicationManager manager) {
		super(manager);
	}
	
	 private SortedListOf<ContactData> cacheContacts;
	
	
	 public SortedListOf<ContactData> getContacts() {
		 if (cacheContacts==null){
			 rebuildCache();
		 }
		 return cacheContacts;
	  }
	 
	 
	public void rebuildCache() {
		 cacheContacts = new SortedListOf<ContactData>();
		 manager.navigateTo().mainPage();
		 List<WebElement> elements = driver.findElements(
            By.xpath(".//table[@id='maintable']/tbody/tr[@class='odd' or @class='even']"));
	     for (WebElement element : elements) {
	             	cacheContacts.add(new ContactData()
	            	 .withName(element.findElement(By.xpath("td[2]")).getText())
                     .withLastname(element.findElement(By.xpath("td[3]")).getText()));
	        }
	}

   
	public ContactHelper createContact(ContactData contact) {
		manager.navigateTo().mainPage();
		initCreationContact();
    	fillContactForm(contact, CREATION);
    	submitCreationContact();
    	returnToHomepage();
    	rebuildCache();
    	return this;
	}

		public  ContactHelper modifyContacts(int index, ContactData contact) {
		manager.navigateTo().mainPage();
		checkContactDetails(index);
		initContactDetailsModification();
    	fillContactForm(contact, MODIFICATION);
    	modificateContactForm();
    	returnToHomepage();
    	rebuildCache();
    	return this;
	}

	public ContactHelper deleteContacts(int index) {
		manager.navigateTo().mainPage();
		initContactModification(index);
		submitContactRemoval();
		returnToHomepage();
		rebuildCache();
		return this;
	}

	
//--------------------------------------------------------------
	
	public ContactHelper initCreationContact() {
			click(By.linkText("add new"));
	return this;
	}

	public ContactHelper fillContactForm(ContactData contact, boolean formType) {
		type(By.name("firstname"), contact.getName());
		type(By.name("lastname"), contact.getLastname());
	  	type(By.name("address"), contact.getMycity());
	  	type(By.name("home"), contact.getHomePhone());
	   	type(By.name("mobile"), contact.getMobilePhone());
	    type(By.name("work"), contact.getWorkPhone());
	    type(By.name("email"), contact.getEmail());
		type(By.name("email2"), contact.getEmail2());

		//selectByText(By.name("bday"), contact.getBirthDay());
	    //selectByText(By.name("bmonth"), contact.getBirthMonth());
		//type(By.name("byear"), contact.getBirthYear());
		if (formType == CREATION){
			 selectByText(By.name("new_group"), "group");
		} else {
			if(driver.findElements(By.name("new_group")).size() != 0) {
				throw new Error("Group selector exists in contact modification form"); 
			}
		}
	   	type(By.name("address2"), contact.getAddress());
	   	type(By.name("phone2"), contact.getHomePhone2());
	   	return this;
	}

	

	public ContactHelper submitCreationContact() {
		click(By.name("submit"));
		cacheContacts = null;
		return this;
	}
	public ContactHelper returnToHomepage() {
		click(By.linkText("home page"));
		return this;
	}

	 public ContactHelper selectContactByIndex(int index) {
         click(By.xpath("//tr[" + (index+2) + "]/td/input"));
         return this;
	 }
	 
		
	public ContactHelper initContactModification(int index) {
		click(By.xpath("//*[@id='maintable']/tbody/tr[" + (index+2) + "]/td[7]/a/img"));
        return this;
	}
	
	public ContactHelper modificateContactForm() {
		click(By.xpath("(//input[@name='update'])"));
		cacheContacts = null;
		return this;
	}
	
	
	public ContactHelper submitContactRemoval() {
         click(By.xpath("//input[@value='Delete']"));
         cacheContacts = null;
         return this;
	
	}
	 public ContactHelper checkContactDetails(int index) {
         selectContactByIndex(index);
         click(By.xpath("//*[@id='maintable']/tbody/tr[" + (index+2) + "]/td[6]/a/img"));        
         return this;
	 }
	 public ContactHelper initContactDetailsModification() {
	        click(By.name("modifiy"));      
	        return this;
	        }
}
